package com.iyao_app.adapter;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.StoryDetail;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.util.TimeFormat;
import com.iyaowoqu.base_library.util.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/22.
 */

public class SignListAdapter extends BaseQuickAdapter<StoryDetail, SignListAdapter.ViewHolder> {


    public SignListAdapter(@Nullable List<StoryDetail> data) {
        super(R.layout.item_sign, data);
    }

    @Override
    protected void convert(final ViewHolder helper, StoryDetail item) {
        if (item.isLast) {
            helper.mLinTime.setVisibility(View.VISIBLE);
            helper.mTvTime.setText("哎吆, 没有更多了哦~ ");
            helper.mRlRoot.setVisibility(View.GONE);
            helper.mTvWeek.setText("");
        } else {

            helper.mRlRoot.setVisibility(View.VISIBLE);
            if (item.isNotDayStart) {
                helper.mLinTime.setVisibility(View.GONE);
            } else {
                helper.mLinTime.setVisibility(View.VISIBLE);
            }
            helper.mTvTime.setText(TimeFormat.getSignTime(item.createTime));
            helper.mTvWeek.setText(String.format("星期%s", TimeUtils.getChineseWeek(item.createTime)));
            try {

                Uri uri = Uri.parse(item.avatarUrl);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(R.dimen.x60, R.dimen.x60))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(helper.mSdvHead.getController())
                        .setImageRequest(request)
                        .build();
                helper.mSdvHead.setController(controller);
            } catch (Exception e) {
                e.printStackTrace();
            }
            helper.mTvName.setText(item.nickname);

            helper.mTvNameTime.setText(TimeFormat.getTime(item.createTime));

            if (TextUtils.isEmpty(item.eventDesc)) {
                helper.mLinV2.setVisibility(View.GONE);
            } else {
                helper.mLinV2.setVisibility(View.VISIBLE);
                helper.mTvContent.setText(item.eventDesc);
            }


            if (item.eventFileUrlList != null && item.eventFileUrlList.size() > 0) {

                final ViewGroup.LayoutParams layoutParams = helper.mSdvPhoto.getLayoutParams();
                final int viewWith = (int) ResourcesUtils.getDimension(R.dimen.x470);
                ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                        if (imageInfo == null) {
                            return;
                        }
                        int height = imageInfo.getHeight();
                        int width = imageInfo.getWidth();

                        layoutParams.height = (int) ((float) (viewWith * height) / (float) width);
                        layoutParams.width = viewWith;
                        helper.mSdvPhoto.setLayoutParams(layoutParams);
                    }

                    @Override
                    public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {

                    }

                    @Override
                    public void onFailure(String id, Throwable throwable) {
                        throwable.printStackTrace();
                    }
                };

                try {
                    Uri uri = Uri.parse(item.eventFileUrlList.get(0));
                    ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                            .setResizeOptions(new ResizeOptions(viewWith, layoutParams.height))
                            .build();
                    PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                            .setOldController(helper.mSdvPhoto.getController())
                            .setImageRequest(request)
                            .setControllerListener(controllerListener)
                            .build();
                    helper.mSdvPhoto.setController(controller);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                helper.mLinV3.setVisibility(View.VISIBLE);
            } else {
                helper.mLinV3.setVisibility(View.GONE);
            }
            if(TextUtils.isEmpty(item.eventDesc)&&item.eventFileUrlList == null ||item.eventFileUrlList.size() == 0){
                helper.mVLine1.setVisibility(View.VISIBLE);
            }else {
                helper.mVLine1.setVisibility(View.GONE);
            }

        }
    }

    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_week)
        TextView mTvWeek;
        @BindView(R.id.lin_time)
        LinearLayout mLinTime;
        @BindView(R.id.v_line)
        View mVLine;
        @BindView(R.id.sdv_head)
        SimpleDraweeView mSdvHead;
        @BindView(R.id.tv_name)
        EmojiconTextView mTvName;
        @BindView(R.id.tv_name_time)
        TextView mTvNameTime;
        @BindView(R.id.tv_content)
        EmojiconTextView mTvContent;
        @BindView(R.id.lin_v_2)
        LinearLayout mLinV2;
        @BindView(R.id.sdv_photo)
        SimpleDraweeView mSdvPhoto;
        @BindView(R.id.lin_v_3)
        LinearLayout mLinV3;
        @BindView(R.id.rl_root)
        LinearLayout mRlRoot;
        @BindView(R.id.v_line_1)
        View mVLine1;


        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
